package net.lab1024.sa.admin.module.business.event.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import net.lab1024.sa.admin.module.business.event.dao.EventDao;
import net.lab1024.sa.admin.module.business.event.domain.entity.EventEntity;
import net.lab1024.sa.admin.module.business.event.domain.form.EventAddForm;
import net.lab1024.sa.admin.module.business.event.domain.form.EventQueryForm;
import net.lab1024.sa.admin.module.business.event.domain.form.EventUpdateForm;
import net.lab1024.sa.admin.module.business.event.domain.vo.EventVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * 事件表 Service
 *
 * @Author 骆伟林
 * @Date 2025-11-04 09:05:22
 * @Copyright 软件需求工程第八组
 */

@Service
public class EventService {

    @Resource
    private EventDao eventDao;

    /**
     * 分页查询
     */
    public PageResult<EventVO> queryPage(EventQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<EventVO> list = eventDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(EventAddForm addForm) {
        EventEntity eventEntity = SmartBeanUtil.copy(addForm, EventEntity.class);
        eventDao.insert(eventEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(EventUpdateForm updateForm) {
        EventEntity eventEntity = SmartBeanUtil.copy(updateForm, EventEntity.class);
        eventDao.updateById(eventEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        eventDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(String eventId) {
        if (null == eventId){
            return ResponseDTO.ok();
        }

        eventDao.updateDeleted(eventId, true);
        return ResponseDTO.ok();
    }

    /**
     * 完成事件，修改状态为完成status字段为FINISHED
     */
    public ResponseDTO<String> finish(Long eventId) {
        eventDao.update(new LambdaUpdateWrapper<EventEntity>().eq(EventEntity::getEventId, eventId)
                .set(EventEntity::getStuats, "FINISHED"));
        return ResponseDTO.ok();
    }

    /**
     * 撤回完成状态
     */
    public ResponseDTO<String> revokeFinish(Long eventId) {
        eventDao.update(new LambdaUpdateWrapper<EventEntity>().eq(EventEntity::getEventId, eventId)
                .set(EventEntity::getStuats, "UNFINISHED"));
        return ResponseDTO.ok();
    }

    /**
     * 根据地址id获取事件列表未完成的 事件
     */

    public List<EventVO> getEventListByAddressId(Long addressId) {
        return SmartBeanUtil.copyList(
                eventDao.selectList(new LambdaQueryWrapper<EventEntity>().eq(EventEntity::getAddressId, addressId)
                        .eq(EventEntity::getDeletedFlag, false)
                        .eq(EventEntity::getStuats, "UNFINISHED")), EventVO.class);
    }

    /**
     * 根据事件id获得事件详情
     */
    public EventVO getEventById(Long eventId) {
        return SmartBeanUtil.copy(eventDao.selectById(eventId), EventVO.class);
    }
}
